var fs = require('fs');
fs.readFile('./json/data.json', 'utf8', function(err, rawData) {
	data = JSON.parse(rawData);
});
//FUNCTION THAT RETURNS A JSON FILE FOR THE SUNBURST (AREAS > RESPONSIBLES > PROJECTS)
exports.sunburstARP = function(req, res) {
	/////////CREATE SUNBURST OBJECT, WHICH WILL CONTAINS ALL AREAS IN HIS CHILDREN LIST//////////////////////////
	var sunburst = {
		"name" : "sunburst",
		"typeOf" : "sunburst",
		"children" : []
	};
	/////////CREATE AREA OBJECTS AND INSERT THEM IN THE CHILDREN OF SUNBURST//////////////////////////
	//this list contains all the area we saw
	var areasList = [];
	for(var i = 0; i < data.items.length; i++) {
		//this list contains all the responsible we saw
		var responsibles = [];
		if(data.items[i].researchAreas != undefined) {
			//if the area isn't undefined create an area object
			var area = {
				"name" : data.items[i].researchAreas[0],
				"typeOf" : "area",
				"children" : []
			}
			//if the area object isn't yet in the sunburst children list add it
			if(!presentInList(areasList, area.name)) {
				areasList.push(area.name);
				sunburst.children.push(area);
			}
		}
		/////////CREATE RESPONSIBLE OBJECTS AND INSERT THEM IN THE CHILDREN OF THE RESPECTIVED AREA/////////////////////////
		for(var k = 0; k < areasList.length; k++) {
			//go through all the areas
			for(var j = 0; j < data.items.length; j++) {
				//go through all the data
				if(data.items[j].researchAreas != undefined) {
					//check if the item is a project
					if(area.name == data.items[j].researchAreas[0]) {
						//create a responsible object
						var responsible = {
							"name" : getName(data.items[j].responsible),
							"typeOf" : "responsible",
							"children" : []
						}
						//add the responsible object if it isn't yet in the area children list
						if(!presentInList(responsibles, responsible.name)) {
							responsibles.push(responsible.name);
							area.children.push(responsible);
						}
						////////CREATE PROJECTS OBJECT TO INSERT IN THE CHILDREN OF EVERY RESPONSIBLE/////////
						for(var l = 0; l < data.items.length; l++) {
							//go through all the data,
							//if the item has the same responsible of the responsible object and the same research area,
							//add the project in the responsible children list
							if(responsible.name == getName(data.items[l].responsible) && area.name == data.items[l].researchAreas[0]) {
								data.items[l].size = data.items[l].months;
								responsible.children.push(data.items[l]);
							}
						};
					}
				}
			};
		};
	};
	res.send(sunburst);
	//write the json file
	fs.writeFile('sunburstARP.json', JSON.stringify(sunburst), function(err) {
		if(err)
			throw err;
		console.log('Sunburst saved!');
	});
}
//FUNCTION THAT RETURNS A JSON FILE FOR THE SUNBURST (AREAS > PROJECTS)
exports.sunburstAP = function(req, res) {
	/////////CREATE SUNBURST OBJECT, WHICH WILL CONTAINS ALL AREAS IN HIS CHILDREN LIST//////////////////////////
	var sunburst = {
		"name" : "sunburst",
		"typeOf" : "sunburst",
		"children" : []
	};
	/////////CREATE AREA OBJECTS AND INSERT THEM IN THE CHILDREN OF SUNBURST//////////////////////////
	//this list contains all the area we saw
	var areasList = [];
	for(var i = 0; i < data.items.length; i++) {
		//this list contains all the responsible we saw
		var responsibles = [];
		if(data.items[i].researchAreas != undefined) {
			//if the area isn't undefined create an area object
			var area = {
				"name" : data.items[i].researchAreas[0],
				"typeOf" : "area",
				"children" : []
			}
			//if the area object isn't yet in the sunburst children list add it
			if(!presentInList(areasList, area.name)) {
				areasList.push(area.name);
				sunburst.children.push(area);
			}
		}
		////////CREATE PROJECTS OBJECT TO INSERT IN THE CHILDREN OF SUNBURST/////////
		for(var l = 0; l < data.items.length; l++) {
			//go through all the data,
			//if the item has the same research area,
			//add the project in the sunburst children list
			if(data.items[l].researchAreas != undefined) {
				if(area.name == data.items[l].researchAreas[0]) {
					data.items[l].size = data.items[l].months;
					area.children.push(data.items[l]);
				}
			}
		};
	}
	res.send(sunburst);
	// write the json file
	fs.writeFile('sunburstAP.json', JSON.stringify(sunburst), function(err) {
		if(err)
			throw err;
		console.log('Sunburst saved!');
	});
}
//FUNCTION THAT RETURNS A JSON FILE FOR THE SUNBURST (AREAS > TYPE > PROJECTS)
exports.sunburstATP = function(req, res) {
	/////////CREATE SUNBURST OBJECT, WHICH WILL CONTAINS ALL AREAS IN HIS CHILDREN LIST//////////////////////////
	var sunburst = {
		"name" : "sunburst",
		"typeOf" : "sunburst",
		"children" : []
	};
	/////////CREATE AREA OBJECTS AND INSERT THEM IN THE CHILDREN OF SUNBURST//////////////////////////
	//this list contains all the area we saw
	var areasList = [];
	for(var i = 0; i < data.items.length; i++) {
		//this list contains all the type we saw
		var types = [];
		if(data.items[i].researchAreas != undefined) {
			//if the area isn't undefined create an area object
			var area = {
				"name" : data.items[i].researchAreas[0],
				"typeOf" : "area",
				"children" : [],
			}
			//if the area object isn't yet in the sunburst children list add it
			if(!presentInList(areasList, area.name)) {
				areasList.push(area.name);
				sunburst.children.push(area);
			}
		}
		/////////CREATE TYPE OBJECTS AND INSERT THEM IN THE CHILDREN OF THE RESPECTIVED AREA/////////////////////////
		for(var k = 0; k < areasList.length; k++) {
			//go through all the areas
			for(var j = 0; j < data.items.length; j++) {
				//go through all the data
				if(data.items[j].researchAreas != undefined) {
					//check if the item is a project
					if(area.name == data.items[j].researchAreas[0]) {
						//create a type object
						var type = {
							"name" : getType(data.items[j].fundingSources[0]),
							"typeOf" : "type",
							"children" : []
						}
						//add the type object if it isn't yet in the area children list
						if(!presentInList(types, type.name)) {
							types.push(type.name);
							area.children.push(type);
						}
						////////CREATE PROJECTS OBJECT TO INSERT IN THE CHILDREN OF EVERY RESPONSIBLE/////////
						for(var l = 0; l < data.items.length; l++) {
							//go through all the data,
							//if the item has the same responsible of the responsible object and the same research area,
							//add the project in the responsible children list
							if(data.items[l].fundingSources != undefined) {
								if(type.name == getType(data.items[l].fundingSources[0]) && area.name == data.items[l].researchAreas[0]) {
									data.items[l].size = data.items[l].months;
									type.children.push(data.items[l]);
								}
							}
						};
					}
				}
			};
		};
	};
	res.send(sunburst);
	//write the json file
	fs.writeFile('sunburstATP.json', JSON.stringify(sunburst), function(err) {
		if(err)
			throw err;
		console.log('Sunburst saved!');
	});
}
// RETURN TRUE IF 'K' IS IN THE 'LIST'
var presentInList = function(list, k) {
	var isPresent = false;
	for(var i = 0; i < list.length; i++) {
		if(list[i] === k) {
			isPresent = true;
		}
	};
	return isPresent;
}
//RETURN THE COMPLETE NAME FROM A RESPONSIBLE ID
var getName = function(id) {
	for(var i = 0; i < data.items.length; i++) {
		if(id == data.items[i].id) {
			var name = data.items[i].first_name + " " + data.items[i].last_name;
		}
	};
	return name;
}
//RETURN THE COMPLETE LABEL OF A FOUNDING SOURCE FROM A FOUNDING SOURCE ID
var getType = function(id) {
	for(var i = 0; i < data.items.length; i++) {
		if(id == data.items[i].id) {
			var foundingSource = data.items[i].label;
		}
	};
	return foundingSource;
}


// CREATES A DICTIONARY TO SET THE COLORS OF THE BANDS IN THE TIMELINE //
var colorDict = {
	"Software Engineering" : "#ff7f0e",
	"Distributed Systems" : "#d62728",
	"Embedded Systems" : "#ff9896",
	"Information Systems" : "#2ca02c",
	"Computational Science" : "#98df8a",
	"Theoretical Computer Science" : "#ffbb78",
	"Intelligent Systems" : "#aec7e8",
	"Other" : "#1f77b4"
}

// CREATES A JSON FOR THE TIMELINE, SETTING A STARTING AND AN ENDING DATE FOR EACH PROJECT //
exports.getTime = function(req, res) {
	var events = [];
	var date;
	var result;
	var description = "";
	var color = "";
	for(var i = 0; i < data.items.length; i++) {

		if(data.items[i].researchAreas != undefined) {
			if(data.items[i].researchAreas[0] in colorDict) {
				color = colorDict[data.items[i].researchAreas[0]];
			}
		}

		if(data.items[i].startDate != undefined) {
			date = data.items[i].startDate.split('-');
			var startDate = new Date(date[0], date[1], date[2]);
			var duration = parseInt(data.items[i].months);
			var endDate = new Date(date[0], date[1], date[2]);
			(endDate).setMonth(endDate.getMonth() + duration);
			for(var j = 0; j < data.items.length; j++) {
				if(data.items[i].responsible == data.items[j].id) {
					resp = data.items[j];
					description += '<font color="' + color + '">' + data.items[i].label + '</font><br><br>';
					description += "Responsible: " + resp.first_name + " " + resp.last_name + '<br>';
					//console.log("collaborators");
					if(data.items[i].collaborators != undefined && data.items[i].collaborators.length > 0) {
						console.log("collaborators");
						description += "Collaborators: ";
						for(var k = 0; k < data.items[i].collaborators.length; k++) {
							for(var l = 0; l < data.items.length; l++) {
								if(data.items[i].collaborators[k] == data.items[l].id) {
									console.log(data.items[l].last_name);
									description += data.items[l].first_name + " " + data.items[l].last_name;
									if(k != data.items[i].collaborators.length - 1) {
										description += ", ";
									};
								};
							};
						};
						description += '<br>';
					}
					if(resp.email != "") {
						description += 'Email: ' + resp.email + '<br>';
					}
					if(resp.website != "") {
						description += 'Website: <a href="' + resp.website + '">Link</a><br>';
					}
					if(resp.tel != "") {
						description += 'Tel: ' + resp.tel + '<br>';
					}
					if(resp.fax != "") {
						description += 'Fax: ' + resp.fax + '<br>';
					}
					if(resp.location != "") {
						description += 'Location: ' + resp.location + '<br>';
					}
					if(resp.office != "") {
						description += 'Office: ' + resp.office + '<br>';
					}
					if(resp.biography != "") {
						description += '<br>Bio: ' + resp.biography + '<br>';
					}
					if(resp.image != "") {
						description += '<br><img src="' + resp.image + '"<br>';
					}

				}
			};
			var project = {
				"start" : startDate,
				"end" : endDate,
				"durationEvent" : true,
				"color" : color,
				"title" : data.items[i].label,
				"description" : description
			};
			description = "";
			events.push(project);
		}
	};

	var timeline = {
		"dateTimeFormat" : "Gregorian",
		"events" : events
	}

	fs.writeFile('./json/timeline.json', JSON.stringify(timeline), function(err) {
		if(err)
			throw err;
		console.log('Timeline saved!');
	});

	//res.send(timeline);

}



exports.getBullet = function(req, res){
	var bigRange = 1000
	var range = 80;
	var emb = 0;
	var dist = 0;
	var inf = 0;
	var comp = 0;
	var inte = 0;
	var soft = 0;
	var theo = 0;
	for(i in data.items) {
		if(data.items[i].id[0] == "p" && data.items[i].id[1] == "r") {
			if(data.items[i].researchAreas[0] == "Embedded Systems") {
				emb += parseInt(data.items[i].months);
			}
			if(data.items[i].researchAreas[0] == "Distributed Systems") {
				dist += parseInt(data.items[i].months);
			}
			if(data.items[i].researchAreas[0] == "Information Systems") {
				inf += parseInt(data.items[i].months);
			}
			if(data.items[i].researchAreas[0] == "Computational Science") {
				comp += parseInt(data.items[i].months);
			}
			if(data.items[i].researchAreas[0] == "Intelligent Systems") {
				inte += parseInt(data.items[i].months);
			}
			if(data.items[i].researchAreas[0] == "Software Engineering") {
				soft += parseInt(data.items[i].months);
			}
			if(data.items[i].researchAreas[0] == "Theoretical Computer Science") {
				theo += parseInt(data.items[i].months);
			}
		}
	}	
	bulletGeneral = [{
		"title" : "Embedded Systems",
		"subtitle" : "Months",
		"ranges" : [bigRange, bigRange, bigRange],
		"measures" : [emb, emb],
		"markers" : [emb]
	}, {
		"title" : "Distributed Systems",
		"subtitle" : "Months",
		"ranges" : [bigRange, bigRange, bigRange],
		"measures" : [dist, dist],
		"markers" : [dist]
	}, {
		"title" : "Information Systems",
		"subtitle" : "Months",
		"ranges" : [bigRange, bigRange, bigRange],
		"measures" : [inf, inf],
		"markers" : [inf]
	}, {
		"title" : "Computational Science",
		"subtitle" : "Months",
		"ranges" : [bigRange, bigRange, bigRange],
		"measures" : [comp, comp],
		"markers" : [comp]
	}, {
		"title" : "Intelligent Systems",
		"subtitle" : "Months",
		"ranges" : [bigRange, bigRange, bigRange],
		"measures" : [inte, inte],
		"markers" : [inte]
	},{
		"title" : "Software Engineering",
		"subtitle" : "Months",
		"ranges" : [bigRange, bigRange, bigRange],
		"measures" : [soft, soft],
		"markers" : [soft]
	}, {
		"title" : "Theoretical Computer Science",
		"subtitle" : "Months",
		"ranges" : [bigRange, bigRange, bigRange],
		"measures" : [theo, theo],
		"markers" : [theo]
	}];
	
	
	var bulletEmb = [];
	var n = 0;
	for(i in data.items) {
		if(data.items[i].id[0] == "p" && data.items[i].id[1] == "r") {
			if(data.items[i].researchAreas[0] == "Embedded Systems") {
				var obj = {
					'title' : data.items[i].label,
					'subtitle' : 'Months',
					'ranges' : [range, range, range],
					'measures' : [data.items[i].months, data.items[i].months],
					'markers' : [data.items[i].months]
				};
				bulletEmb.push(obj);
				n += 1;
			}
		}
	}
	
	var bulletDist = [];
	var n = 0;
	for(i in data.items) {
		if(data.items[i].id[0] == "p" && data.items[i].id[1] == "r") {
			if(data.items[i].researchAreas[0] == "Distributed Systems") {
				var obj = {
					'title' : data.items[i].label,
					'subtitle' : 'Months',
					'ranges' : [range, range, range],
					'measures' : [data.items[i].months, data.items[i].months],
					'markers' : [data.items[i].months]
				};
				bulletDist.push(obj);
				n += 1;
			}
		}
	}
	
	var bulletInfor = [];
	var n = 0;
	for(i in data.items) {
		if(data.items[i].id[0] == "p" && data.items[i].id[1] == "r") {
			if(data.items[i].researchAreas[0] == "Information Systems") {
				var obj = {
					'title' : data.items[i].label,
					'subtitle' : 'Months',
					'ranges' : [range, range, range],
					'measures' : [data.items[i].months, data.items[i].months],
					'markers' : [data.items[i].months]
				};
				bulletInfor.push(obj);
				n += 1;
			}
		}
	}
	
	var bulletComp = [];
	var n = 0;
	for(i in data.items) {
		if(data.items[i].id[0] == "p" && data.items[i].id[1] == "r") {
			if(data.items[i].researchAreas[0] == "Computational Science") {
				var obj = {
					'title' : data.items[i].label,
					'subtitle' : 'Months',
					'ranges' : [range, range, range],
					'measures' : [data.items[i].months, data.items[i].months],
					'markers' : [data.items[i].months]
				};
				bulletComp.push(obj);
				n += 1;
			}
		}
	}
	
	var bulletInte = [];
	var n = 0;
	for(i in data.items) {
		if(data.items[i].id[0] == "p" && data.items[i].id[1] == "r") {
			if(data.items[i].researchAreas[0] == "Intelligent Systems") {
				var obj = {
					'title' : data.items[i].label,
					'subtitle' : 'Months',
					'ranges' : [range, range, range],
					'measures' : [data.items[i].months, data.items[i].months],
					'markers' : [data.items[i].months]
				};
				bulletInte.push(obj);
				n += 1;
			}
		}
	}
	
	var bulletSoftw = [];
	var n = 0;
	for(i in data.items) {
		if(data.items[i].id[0] == "p" && data.items[i].id[1] == "r") {
			if(data.items[i].researchAreas[0] == "Software Engineering") {
				var obj = {
					'title' : data.items[i].label,
					'subtitle' : 'Months',
					'ranges' : [range, range, range],
					'measures' : [data.items[i].months, data.items[i].months],
					'markers' : [data.items[i].months]
				};
				bulletSoftw.push(obj);
				n += 1;
			}
		}
	}
	
	var bulletTheo = [];
	var n = 0;
	for(i in data.items) {
		if(data.items[i].id[0] == "p" && data.items[i].id[1] == "r") {
			if(data.items[i].researchAreas[0] == "Theoretical Computer Science") {
				var obj = {
					'title' : data.items[i].label,
					'subtitle' : 'Months',
					'ranges' : [range, range, range],
					'measures' : [data.items[i].months, data.items[i].months],
					'markers' : [data.items[i].months]
				};
				bulletTheo.push(obj);
				n += 1;
			}
		}
	}
	
	

	
	
	fs.writeFile('./json/bulletGeneral.json', JSON.stringify(bulletGeneral) , function (err) {
 	 if (err) throw err;
 	 console.log('Bullet saved!');
	});
	
	fs.writeFile('./json/bulletEmb.json', JSON.stringify(bulletEmb) , function (err) {
 	 if (err) throw err;
 	 console.log('Bullet saved!');
	});
	
	fs.writeFile('./json/bulletDist.json', JSON.stringify(bulletDist) , function (err) {
 	 if (err) throw err;
 	 console.log('Bullet saved!');
	});
	
	fs.writeFile('./json/bulletInfor.json', JSON.stringify(bulletInfor) , function (err) {
 	 if (err) throw err;
 	 console.log('Bullet saved!');
	});
	
	fs.writeFile('./json/bulletComp.json', JSON.stringify(bulletComp) , function (err) {
 	 if (err) throw err;
 	 console.log('Bullet saved!');
	});
	
	fs.writeFile('./json/bulletInte.json', JSON.stringify(bulletInte) , function (err) {
 	 if (err) throw err;
 	 console.log('Bullet saved!');
	});
	
	fs.writeFile('./json/bulletSoftw.json', JSON.stringify(bulletSoftw) , function (err) {
 	 if (err) throw err;
 	 console.log('Bullet saved!');
	});
	
	fs.writeFile('./json/bulletTheo.json', JSON.stringify(bulletTheo) , function (err) {
 	 if (err) throw err;
 	 console.log('Bullet saved!');
	});
	
	res.send(bulletGeneral);
	
	

}

